package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// Location 位置
type Location struct {
	ID           string  `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	Name         string  `json:"name" gorm:"column:name;type:string;size:40;index:name,unique;comment:位置名称"`
	ProvinceID   string  `json:"province_id" gorm:"column:province_id;type:int;size:64;comment:省份id"`
	CityID       string  `json:"city_id" gorm:"column:city_id;type:int;size:64;comment:城市id"`
	DistrictID   string  `json:"district_id" gorm:"column:district_id;type:int;size:64;comment:区id"`
	Addr         string  `json:"addr" gorm:"column:addr;type:string;size:100;comment:详细地址"`
	EnableNav    int     `json:"enable_nav" gorm:"column:enable_nav;type:int;size:8;default:0;comment:开启导航,0-关闭,1-开启"`
	Token        string  `json:"token" gorm:"column:token;type:string;size:100;default:'';comment:商场的Token"`
	RobotIds     string  `json:"robot_ids" gorm:"column:robot_ids;type:string;size:200;default:'';comment:当前商场可查看机器列表，逗号分隔"`
	NavURL       string  `json:"nav_url" gorm:"column:nav_url;type:string;size:200;comment:外部导航链接"`
	Latitude     float64 `json:"latitude" gorm:"column:latitude;type:decimal(9,6);comment:坐标,维度"`
	Longitude    float64 `json:"longitude" gorm:"column:longitude;type:decimal(9,6);comment:坐标,经度"`
	OpeningHours string  `json:"opening_hours" gorm:"column:opening_hours;type:string;size:20;comment:营业时间"`
	CreatedAt    int64   `json:"created_at" gorm:"autoCreateTime;column:created_at;type:int;size:64;comment:创建时间"`
	UpdatedAt    int64   `json:"updated_at" gorm:"autoUpdateTime;column:updated_at;type:int;size:64;comment:更新时间"`
}

// TableName 表名
func (Location) TableName() string {
	return "location"
}

// BeforeCreate 自增id
func (l *Location) BeforeCreate(tx *gorm.DB) (err error) {
	if l.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	l.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &Location{})
}
